
import pandas as pd
import numpy as np

def calc_corrs(se1, se2, tag):
  c1 = se1.corr(se2, method='pearson')
  c2 = se1.corr(se2, method='spearman')
  c3 = se1.corr(se2, method='kendall')
  print(tag + ", c1:" + str(c1) + ", c2:" + str(c2) + ", c3:" + str(c3))
  return c1,c2,c3

def card_cal(prob, base_point=50, odds=1/19, pdo=30, keep=3):
  if prob < 0:
    return prob
    
  B = pdo / np.log(2)
  A = base_point + B * np.log(odds)
  y = np.log(prob / (1-prob))
  score = round(A-B*y, keep)
  
  s1 = score
  s1 = round(s1)
  if s1<0:
    s1 = 0
  if s1>100:
    s1 = 100
  return s1


pdPtn = pd.read_csv("./子分V1.1测试结果_20241210.csv")
print(pdPtn)
print(pdPtn.columns.tolist())

vQhmFeats = []
for line in open("D:/yrProj/202406zhongan/青禾苗-模型文件/银融_青禾苗_三版模型/银融_青禾苗_入模特征_子分1.1.txt"):
  line = line.strip()
  vQhmFeats.append(line)
print(vQhmFeats)
print(len(vQhmFeats))

vEmpty = []
for idx,row in pdPtn.iterrows():
  isAllEmpty = True
  for fc in vQhmFeats:
    if np.isnan(row[fc]):
      pass
    else:
      isAllEmpty = False
      break
  if isAllEmpty:
    vEmpty.append(1)
  else:
    vEmpty.append(0)
    
#print(vEmpty)
print(len(vEmpty))
print(np.sum(vEmpty))
#exit()
    

#pxPath = "./zzPx"
#tsPath = "./zzTs"

#pxPath = "./y1Px"
#tsPath = "./y1Ts"

#pxPath = "./y2Px"
#tsPath = "./y2Ts"

#pxPath = "./y3Px"
#tsPath = "./y3Ts"

pxPath = "./hrPx"
tsPath = "./hrTs"

vSc = []
vTs = []
dTsFreq = {}
dTsSegFreq = {}
fOut = open(tsPath, "w")
lIdx = 0
for line in open(pxPath):
  line = line.strip()
  rs = float(line)
  isEmpty = vEmpty[lIdx]
  if isEmpty == 1: #False: #isEmpty == 1:
    rs = -1
    ts = -1
  else:
#    ts = card_cal(rs, base_point=70, odds=0.2, pdo=20)*6 + 300  # zz
#    ts = card_cal(rs, base_point=75, odds=0.02, pdo=45)*6 + 300  # y1
#    ts = card_cal(rs, base_point=75, odds=0.07, pdo=40)*6 + 300  # y2
#    ts = card_cal(rs, base_point=85, odds=0.05, pdo=60)*6 + 300  # y3
    ts = card_cal(rs, base_point=95, odds=0.03, pdo=50) * 6 + 300 # hr

  
  fOut.write(str(rs) + "\t" + str(ts) + "\n")
  
  vSc.append(rs)
  vTs.append(ts)
  dTsFreq[ts] = dTsFreq.get(ts, 0) + 1
  
  tsSeg = int(ts/10)
  dTsSegFreq[tsSeg] = dTsSegFreq.get(tsSeg, 0) + 1
  
  lIdx += 1

fOut.close()
calc_corrs(pd.Series(vSc), pd.Series(vTs), 'aa')

sScFreq = sorted(dTsFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreq:
    print(str(kv[0]) + "," + str(kv[1]))
print("###############")
sScFreqS = sorted(dTsSegFreq.items(), key=lambda x: x[0], reverse=False)
for kv in sScFreqS:
    print(str(kv[0]) + "," + str(kv[1]))

  
